package com.atguigu.system.service.impl;

import com.atguigu.system.mapper.InventoryInfoMapper;
import com.atguigu.system.model.entity.InventoryInfo;
import com.atguigu.system.model.entity.Sku;
import com.atguigu.system.model.entity.Spu;
import com.atguigu.system.service.InventoryInfoService;
import com.atguigu.system.service.SkuService;
import com.atguigu.system.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-01
 */
@Service
public class InventoryInfoServiceImpl extends ServiceImpl<InventoryInfoMapper, InventoryInfo> implements InventoryInfoService {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Override
    public Page<InventoryInfo> pageQuery(Page<InventoryInfo> page, Integer skuId) {
        LambdaQueryWrapper<InventoryInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (skuId != null) {
            queryWrapper.eq(InventoryInfo::getSkuId, skuId);
        }
        queryWrapper.eq(InventoryInfo::getIsDeleted, 0);

        Page<InventoryInfo> inventoryInfoPage = baseMapper.selectPage(page, queryWrapper);

        // 库存SKU改成具体信息
        List<InventoryInfo> records = inventoryInfoPage.getRecords();
        records.forEach(i->{
            String desc = null;
            Sku byId = skuService.selectByID(i.getSkuId());
            Spu spu = spuService.selectByID(byId.getSpuId());

            String spuName = spu.getSpuName();
            String brand = spu.getBrand();
            desc = spuName+"-"+brand+"-"+byId.getColor()+"-"+byId.getSize()+"-"+byId.getQuality();
            i.setSkuName(desc);

        });
        inventoryInfoPage.setRecords(records);
        return inventoryInfoPage;
    }
}
